#include "contact.h"

static int SearchByName(char* name, con* pContact)
{
	int i = 0;
	for (i = 0; i < pContact->sz; i++)
	{
		if (0 == strcmp(name, pContact->arr[i].name))
		{
			return i;
		}
	}
	return -1;
}

int CompareByName(const void* a, const void* b)
{
	return strcmp(((info*)a)->name, ((info*)b)->name);
}

void Menu()
{
	printf("*****************************\n");
	printf("*********  Contacts  ********\n");
	printf("*****************************\n");
	printf("***  1.Add      2.Delete  ***\n");
	printf("***  3.Search   4.Modify  ***\n");
	printf("***  5.Sort     6.Display ***\n");
	printf("***        0.Exit         ***\n");
	printf("*****************************\n");

}

void Init(con* pContact)
{
	memset(pContact->arr, 0, sizeof(pContact->arr));
}

void Add(con* pContact)
{
	if (pContact->sz == ARR_M)
	{
		printf("The contacts is full.\n");
		return;
	}

	printf("Type the name.\n");
	scanf("%[^\n]", (pContact->arr)[pContact->sz].name);
	while (getchar() != '\n');

	printf("Type the sex(0:Male/1:Female/2:Other).\n");
	scanf("%d", &(pContact->arr)[pContact->sz].sex);
	while (getchar() != '\n');

	printf("Type the age.\n");
	scanf("%d", &(pContact->arr)[pContact->sz].age);
	while (getchar() != '\n');

	printf("Type the tel.\n");
	scanf("%[^\n]", (pContact->arr)[pContact->sz].tel);
	while (getchar() != '\n');

	printf("Type the address.\n");
	scanf("%[^\n]", (pContact->arr)[pContact->sz].address);
	while (getchar() != '\n');

	pContact->sz++;
	printf("Successfully added\n");
}

void Delete(con* pContact)
{
	if (0 == pContact->sz)
	{
		printf("There's nothing to delete here.\n");
	}

	char name[NAME_M] = { 0 };
	printf("Type a contact to delete:>");
	scanf("%[^\n]", name);
	int i = 0;
	int pos = SearchByName(name, pContact);

	if (-1 == pos)
	{
		printf("Tere's no one called %s.\n", name);
		return;
	}

	for (; pos < (pContact->sz) - 1; pos++)
	{
		pContact->arr[pos] = pContact->arr[pos + 1];
	}
	printf("Successfully deleted.\n");
}

void Search(const con* pContact)
{
	char name[NAME_M] = { 0 };
	printf("Type a name to search:>");
	scanf("%[^\n]", name);
	int i = SearchByName(name, pContact);
	if (-1 != i)
	{
		printf("Results are as follows:\n");
		printf("%-20s%-8s%-5s%-12s%-20s\n", "name", "sex", "age", "tel", "address");
		printf("%-20s", pContact->arr[i].name);
		if (0 == pContact->arr[i].sex)
		{
			printf("%-8s", "Male");
		}
		else if (1 == pContact->arr[i].sex)
		{
			printf("%-8s", "Female");
		}
		else
		{
			printf("%-8s", "Other");
		}
		printf("%-5d", pContact->arr[i].age);
		printf("%-12s", pContact->arr[i].tel);
		printf("%-20s\n", pContact->arr[i].address);
	}
	else
	{
		printf("Nothing was found.\n");
	}

}

void Modify(con* pContact)
{
	char name[NAME_M] = { 0 };
	printf("Type a name to modify:>");
	scanf("%[^\n]", name);
	int pos = SearchByName(name, pContact);
	if (-1 == pos)
	{
		printf("Tere's no one called %s.\n", name);
	}
	else
	{
		printf("Type the name.\n");
		scanf("%[^\n]", (pContact->arr)[pos].name);
		while (getchar() != '\n');

		printf("Type the sex(0:Male/1:Female/2:Other).\n");
		scanf("%d", &(pContact->arr)[pos].sex);
		while (getchar() != '\n');

		printf("Type the age.\n");
		scanf("%d", &(pContact->arr)[pos].age);
		while (getchar() != '\n');

		printf("Type the tel.\n");
		scanf("%[^\n]", (pContact->arr)[pos].tel);
		while (getchar() != '\n');

		printf("Type the address.\n");
		scanf("%[^\n]", (pContact->arr)[pos].address);
		while (getchar() != '\n');

		printf("Successfully modified.\n");
	}
}

void Sort(con* pContact)
{
	qsort(pContact->arr, pContact->sz, sizeof(info), CompareByName);
	printf("Successfully sorted.\n");
}

void Display(const con* pContact)
{
	int i = 0;
	printf("%-20s%-8s%-5s%-12s%-20s\n", "name", "sex", "age", "tel", "address");

	for (; i < pContact->sz; i++)
	{
		printf("%-20s", pContact->arr[i].name);
		if (0 == pContact->arr[i].sex)
		{
			printf("%-8s", "Male");
		}
		else if (1 == pContact->arr[i].sex)
		{
			printf("%-8s", "Female");
		}
		else
		{
			printf("%-8s", "Other");
		}
		printf("%-5d", pContact->arr[i].age);
		printf("%-12s", pContact->arr[i].tel);
		printf("%-20s\n", pContact->arr[i].address);
	}
}

